<?php
namespace Home\Controller;
use Think\Controller;
//
class ScanController extends CommonController {
	public function index(){
		$page_no = I('post.page') ? I('post.page') : 1;
		//获取用户id
		$where['user_id'] = get_user();
		
		$scan_id = M('scan_log')->where(array('id'=>$where['user_id']))->getField('max(id)');
		if(!$scan_id){
			$this->assign('list',array());
			$this->assign('scan',0);
			$this->assign('shopname','目前还没有扫描记录');
			$this->assign('not_scan',0);
			$this->display();
			return false;
		}
		$log = M('scan_log')->where('id='.$scan_id)->find();
		if(!$log){
			$this->assign('list',array());
			$this->assign('scan',0);
			$this->assign('shopname','目前还没有扫描记录');
			$this->assign('not_scan',0);
			$this->display();
			return false;
		}
		$shop_id = M('device')->where('id='.$log['device_id'])->getField('shop_id');
		$shopname = M('shop')->where(array('id'=>$shop_id))->getField('shop_name');
		$where['shop_id'] = $shop_id;
		$scan = M('device')->where(array('shop_id'=>$shop_id,'status'=>1))
			->count();
		$not_scan = M('device')->where(array('shop_id'=>$shop_id,'status'=>0))
			->count();
		$list = M('scan_log a')->page($page_no,10)
			->field('a.id,a.add_time,a.device_id,b.name,b.area')
			->join('left join zm_device b on a.device_id=b.id')
			->order('a.id desc')
			->where($where)
			->select();
		if (IS_POST) {
			$this->ajaxReturn($list);
		}
		$this->assign('list',$list);
		$this->assign('scan',$scan);
		$this->assign('shopname',$shopname);
		$this->assign('not_scan',$not_scan);
		$this->display();
	}
	public function task(){
		$page_no = I('post.page') ? I('post.page') : 1;
		//获取用户id
		$where['user_id'] = get_user();

		$scan_id = M('scan_log')->where(array('id'=>$where['user_id']))->getField('max(id)');
		$log = M('scan_log')->where('id='.$scan_id)->find();
		$shop_id = M('device')->where('id='.$log['device_id'])->getField('shop_id');
		$total = M('device')
			->where('shop_id='.$shop_id.' AND status=0')
			->count();
		$list = M('device a')->page($page_no,10)
			->field('a.id,a.add_time,a.name,a.area')
			->order('a.id desc')
			->where('shop_id='.$shop_id.' AND status=0')
			->select();
		if (IS_POST) {
			$this->ajaxReturn($list);
		}
		$this->assign('list',$list);
		$this->assign('total',$total);
		$this->display();
	}
}